package com.wwx.worldonwheels.service.impl;


import com.wwx.worldonwheels.VO.CustomerVO;
import com.wwx.worldonwheels.VO.ModifyInfoVo;
import com.wwx.worldonwheels.common.Constant;
import com.wwx.worldonwheels.common.result.CommonResult;
import com.wwx.worldonwheels.domain.*;
import com.wwx.worldonwheels.mapper.*;
import com.wwx.worldonwheels.service.CustomerLoginRegisterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * ClassName: UserService
 * Description:
 * CreatedBy: HuaHX
 */
@Slf4j
@Service
public class CustomerLoginRegisterServiceImpl implements CustomerLoginRegisterService {


    @Autowired
    private WwxCustomerMapper customerMapper;
    @Autowired
    private WwxCorporateMapper corporateMapper;
    @Autowired
    private WwxIndividualMapper individualMapper;
    @Autowired
    private WwxInvoiceMapper invoiceMapper;
    @Autowired
    private WwxPaymentMapper paymentMapper;

    @Override
    public String register(CustomerVO user) {
        log.info("入参是{}", user.toString());
        user.setPassword(user.getPassword());
        log.info("密码是{}", user.getPassword());

        WwxCustomer customer = new WwxCustomer(
                user.getCustId(),
                user.getAddrStreet(),
                user.getAddrCity(),
                user.getAddrZip(),
                user.getAddrCountry(),
                user.getEmail(),
                user.getPhone(),
                user.getCustType(),
                user.getPassword()
        );
       //插入customer
//        if(!Objects.equals(customer.getCustType(), "1") || !Objects.equals(customer.getCustType(), "0")) return
        customerMapper.insertSelective(customer);

        if (Constant.CORPORATE_TYPE.equals(user.getCustType())) {

            WwxCorporate corporate = new WwxCorporate(
                    customer.getCustId(),
                    user.getName(),
                    user.getRegisNo(),
                    user.getEmpId(),
                    user.getAccEmail()
            );
            corporateMapper.insertSelective(corporate);
        } else if (Constant.INDIVIDUAL_TYPE.equals(user.getCustType())) {

         WwxIndividual individual=new WwxIndividual(
                 customer.getCustId(),
                 user.getFname(),
                 user.getMname(),
                 user.getLname(),
                 user.getLicenseNo(),
                 user.getInsurName(),
                 user.getInsurNo()
         );
         individualMapper.insertSelective(individual);

        }

        return "成功注册，ID为"+customer.getCustId();
    }


    @Override
    public CommonResult login(String phone, String password) {
        log.info("入参是phone：{}，password:{}",phone,password);
//        //todo: 为什么example失效
//        // res: 参数我写了双引号。3=>"3"
//        WwxCustomerExample customerExample=new WwxCustomerExample();
//        customerExample.or().andPhoneEqualTo(phone).andPasswordEqualTo(password);
//        List<WwxCustomer> customer= customerMapper.selectByExample(customerExample);
//                log.info(customer.toString());
//        if(customer==null||customer.size()==0){
//            return CommonResult.failed("phone or password wrong，please try again");
//        }else{
//            return CommonResult.success(customer.get(0));
//        }


        WwxCustomer customer = customerMapper.selectByPhone(phone);
        if (customer == null) {
            return CommonResult.failed("this user is not exist");
        }
        log.info(customer.toString());
        if (customer.getPassword().equals(password)) {
            return CommonResult.success(customer);
        } else {
            return CommonResult.failed("password is not right");
        }

    }

    /**
     * User information modification by cust_id
     * @param modifyInfoVo information to be modified
     * @return
     */
    @Override
    public CommonResult modifyInformation(ModifyInfoVo modifyInfoVo) {
        log.info("入参为{}",modifyInfoVo);
        if(modifyInfoVo==null){
            return CommonResult.failed("Modify the information to be empty!");
        }
        int flag=customerMapper.updateInfoByCustIdAnd(modifyInfoVo);
        if(flag>0){
            return CommonResult.success(flag,"Modify information successfully");
        }
        return CommonResult.failed("Modify information successfully!");
    }

    /**
     *Information query
     * @param custId
     * @return
     */
    @Override
    public CommonResult getInformation(Integer custId) {
        log.info("getInfo-input:{}",custId);
        WwxCustomer wwxCustomer=customerMapper.selectByPrimaryKey(custId);
        if(wwxCustomer==null){
            return CommonResult.failed("Query failed!");
        }
        log.info("getInfo-info:{}",wwxCustomer);
        ModifyInfoVo modifyInfoVo=new ModifyInfoVo(wwxCustomer);
        log.info("getInfo-info:{}",modifyInfoVo);
        return CommonResult.success(modifyInfoVo,"Query successful!");
    }

    /**
     * Invoice information query
     * @param custId
     * @return
     */
    @Override
    public CommonResult getInvoiceInfo(Integer custId) {
        log.info("getInvoiceInfo-input:{}",custId);
        List<WwxInvoice> invoiceInfoList =invoiceMapper.selectByCustId(custId);
        log.info("getInvoiceInfo-InvoiceInfo:{}",invoiceInfoList);
        if(invoiceInfoList==null){
            return CommonResult.failed("该用户不存在服务记录、发票记录");
        }
        return CommonResult.success(invoiceInfoList,"Invoice record query successful");
    }

    /**
     * Payment information query
     * @param custId
     * @return
     */
    @Override
    public CommonResult getPaymentInfo(Integer custId) {
        List<WwxPayment> paymentInfoList=paymentMapper.selectByCustId(custId);
        if(paymentInfoList==null){
            return CommonResult.failed("Payment information query failed!");
        }
        return CommonResult.success(paymentInfoList,"Payment information query successful!");
    }

}
